<template>
  <div class="app-main-container">
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="queryList"
      @clear="clear"
    >
      <template v-if="meta.indexOf('SignRecordListExport') > -1" #btns>
        <el-button :loading="exportLoading" type="primary" @click="exportExcel">{{ $t('message.daochu') }}</el-button>
      </template>
    </QueryForm>
    <div v-if="meta.indexOf('SignStatusUpdate') > -1" class="doumee_table_header">
      <el-button
        v-preventReClick
        type="primary"
        @click="openModal"
      >{{ $t('message.plhx') }}</el-button>
    </div>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table doumee-element-tableb"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        align="center"
        type="selection"
        :selectable="handleDisable"
        width="55"
      />
      <el-table-column align="center" :label="$t('message.hdname')" show-overflow-tooltip prop="acName" min-width="140" />
      <el-table-column align="center" :label="$t('message.yhxx')" show-overflow-tooltip prop="userInfo" min-width="140" />
      <el-table-column align="center" :label="$t('message.bmry')" show-overflow-tooltip prop="studentName" min-width="140" />
      <el-table-column align="center" :label="$t('message.bmsj')" show-overflow-tooltip prop="createTime" min-width="160" />
      <el-table-column align="center" :label="$t('message.hxzt')" show-overflow-tooltip prop="statusName" min-width="80" />
      <el-table-column align="center" :label="$t('message.hxsj')" show-overflow-tooltip prop="writeOffTime" min-width="160" />
      <el-table-column align="center" :label="$t('message.hxr')" show-overflow-tooltip prop="editor" min-width="120" />
      <el-table-column :label="$t('message.caozuo')" min-width="100" align="center" fixed="right">
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button
              v-if="scope.row.status == 0 && meta.indexOf('SignStatusUpdate') > -1"
              v-preventReClick
              type="text"
              @click="hxSign(scope.row.id)"
            >{{ $t('message.hx') }}</el-button>
            <el-button
              v-else-if="scope.row.status == 1 && meta.indexOf('SignStatusUpdate') > -1"
              v-preventReClick
              type="text"
              class="secondColor"
              @click="cxSign(scope.row.id)"
            >{{ $t('message.chexiao') }}</el-button>
            <span v-else>-</span>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <Paginations
      :total="totalCount"
      :page="pagination.page"
      @current-change="currentChange"
      @SizeChange="handleSizeChange"
    />
    <el-dialog
      :title="$t('message.plhx')"
      top="20vh !important"
      :visible.sync="isShowAdjust"
      :close-on-click-modal="closeModal"
      width="500px"
    >
      <div class="adjust_modal">
        <el-radio-group v-model="isAllType">
          <div v-if="selectSalesList && selectSalesList.length > 0"><el-radio :label="1">{{ $t('message.yxdq') }}{{ selectSalesList.length }}{{ $t('message.tsj') }}</el-radio></div>
          <div class="mt10"><el-radio :label="2">{{ $t('message.yxdqsx') }}{{ totalCount }}{{ $t('message.tsj') }}</el-radio></div>
        </el-radio-group>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isShowAdjust = false">{{ $t('message.qx') }}</el-button>
        <el-button
          v-preventReClick
          type="primary"
          :loading="subLoading"
          @click="handleModelEnter"
        >{{ $t('message.qr') }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { signRecordList, signStatusUpdate, signRecordListExport, batchHXSign } from '@/api'
import { parseTime, parseTimeb } from '@/utils/index'
import { queryFormConfig } from './config'
import QueryForm from '@/components/QueryForm'
export default {
  name: 'SignRecordList',
  components: {
    QueryForm
  },
  data() {
    return {
      totalCount: 0,
      queryFormConfig,
      isShowAdjust: false,
      subLoading: false,
      closeModal: this.$store.getters.dialog,
      list: [],
      meta: [],
      isAllType: 1,
      filters: {
        times: [parseTimeb(7).slice(0, 10), parseTime(new Date()).slice(0, 10)]
      },
      exportLoading: false,
      listLoading: false,
      pagination: {
        page: 1,
        rows: 10
      },
      selectSalesList: []
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.queryList()
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  methods: {
    handleModelEnter() {
      this.subLoading = true
      const { filters, selectSalesList } = this
      if (filters.times && filters.times.length > 0) {
        filters.startTime = filters.times[0]
        filters.endTime = filters.times[1]
      } else {
        filters.startTime = null
        filters.endTime = null
      }
      let param = {}
      if (this.isAllType === 2 || this.isAllType === '2') {
        param = { ...filters, type: '1' }
      } else {
        param = { ids: selectSalesList.map(i => i.id), type: '0' }
      }
      batchHXSign({ param }).then(res => {
        this.subLoading = false
        if (res.errorCode === '000000') {
          this.$message.success(this.$t('message.plhxcg'))
          this.getList()
          this.isShowAdjust = false
        }
      })
        .catch(() => {
          this.subLoading = false
        })
    },
    handleSelectionChange(val) {
      this.selectSalesList = val
    },
    openModal() {
      if (this.totalCount === 0) {
        return false
      }
      const selectSalesList = this.selectSalesList
      if (selectSalesList.length === 0) {
        this.isAllType = 2
      } else {
        this.isAllType = 1
      }
      this.isShowAdjust = true
    },
    handleDisable(row, index) {
      if (row.status !== '0') {
        return false
      } else {
        return true
      }
    },
    hxSign(id) {
      this.$confirm(this.$t('message.hxbmtc'), this.$t('message.ts'), {
        confirmButtonText: this.$t('message.qd'),
        cancelButtonText: this.$t('message.qx'),
        type: 'warning'
      })
        .then(() => {
          signStatusUpdate({ param: { id, status: '1' }}).then(res => {
            if (res.errorCode === '000000') {
              this.$message.success(this.$t('message.hxcg'))
              this.getList()
            }
          })
        })
        .catch(() => {
        })
    },
    cxSign(id) {
      this.$confirm(this.$t('message.cxbmtc'), this.$t('message.ts'), {
        confirmButtonText: this.$t('message.qd'),
        cancelButtonText: this.$t('message.qx'),
        type: 'warning'
      })
        .then(() => {
          signStatusUpdate({ param: { id, status: '0' }}).then(res => {
            if (res.errorCode === '000000') {
              this.$message.success(this.$t('message.cxcg'))
              this.getList()
            }
          })
        })
        .catch(() => {
        })
    },
    exportExcel() {
      this.exportLoading = true
      const { filters } = this
      if (filters.times && filters.times.length > 0) {
        filters.startTime = filters.times[0]
        filters.endTime = filters.times[1]
      } else {
        filters.startTime = null
        filters.endTime = null
      }
      signRecordListExport({
        param: { ...filters, venueId: sessionStorage.getItem('venueId') }
      })
        .then((res) => {
          this.exportLoading = false
          if (res.errorCode === '000000') {
            window.open(res.record.showUrl, '_blank')
          }
        })
        .catch(() => { this.exportLoading = false })
    },
    getList() {
      const { pagination } = this
      const filters = { ...this.filters }
      if (filters.times && filters.times.length > 0) {
        filters.startTime = filters.times[0]
        filters.endTime = filters.times[1]
      } else {
        filters.startTime = null
        filters.endTime = null
      }
      this.listLoading = true
      signRecordList({ param: filters, pagination }).then(res => {
        this.listLoading = false
        if (res.errorCode === '000000') {
          this.totalCount = res.totalCount
          this.list = res.recordList
          if (res.totalCount && res.recordList.length === 0 && pagination.page > 1) {
            this.getList(Math.ceil(res.totalCount / pagination.rows))
          }
        }
      }, () => {
        this.listLoading = false
      })
    },

    queryList() {
      this.pagination.page = 1
      this.getList()
    },
    clear() {
      this.filters = {
        times: this.filters.times
      }
      this.queryList()
    },
    currentChange(val) {
      this.pagination.page = val
      this.getList()
    },
    handleSizeChange(value) {
      this.pagination.rows = value
      this.queryList()
    }
  }
}
</script>
